package edu.hawaii.duedates.webApp;

import static org.junit.Assert.assertNotNull;

import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
import edu.hawaii.duedates.configuration.DueDatesConfiguration;
import edu.hawaii.duedates.configuration.User;

/**
 * This is a test case for AlertsPage.java.
 * 
 * @author Mari-Lee Flestado, Aric West, Tyler Wolff
 *
 */
public class TestAlertsPage {
  
  /** Config file for testing. */
  private DueDatesConfiguration config;
  
  private static final String ALERTS = "Alerts";
  
  /**
   * Sets up a mock DueDatesConfiguration object for use in
   * tests.
   */
  @Before
  public void setUp() {
    this.config = new DueDatesConfiguration();
    User testUser = new User("Test", "tester@hawaii.edu", "password");
    testUser.setUhManoa("-uhm 12345678 test");
    testUser.setHawaiiStateLib("-hsl 2342 23423");
    this.config.addUser(testUser);
    this.config.setSmtpServer("mail.hawaii.edu");  
  }
  
  /**
   * Testing the basic functionality of the Alerts Page class.
   */
  @Test
  public void testAlertsPage() {
    
    // Start up the WicketTester and check that the startpage renders. 
    WicketTester tester = new WicketTester(new DueDatesApplication(config));
    tester.startPage(Index.class);
    
    // Have to run through login to correctly create user in session
    FormTester formTester = tester.newFormTester("loginForm");
    formTester.setValue("LoginEmail", "tester@hawaii.edu");
    formTester.setValue("LoginPassword", "password");
    formTester.submit("Login");
    tester.assertRenderedPage(ResultPage.class);
    
    // Goto alert page
    tester.clickLink(ALERTS);
    tester.assertRenderedPage(AlertsPage.class);
    
    //Check wicket id's
    assertNotNull("Testing wicket:id Results found.", tester.getTagByWicketId("Results"));
    assertNotNull("Testing wicket:id Alerts found", tester.getTagByWicketId(ALERTS));
    assertNotNull("Testing wicket:id Logout found", tester.getTagByWicketId("Logout"));
    
    // Test that "University" appears somewhere on this page. 
    tester.assertContains("University");
   
    FormTester formTester2 = tester.newFormTester("alertsForm");
    formTester2.setValue("enableAlerts", "true");
    formTester2.setValue("enableUhAlerts", "true");
    formTester2.setValue("enableHslAlerts", "true");   
    formTester2.setValue("daysUntilDue", "3");
    formTester2.submit("updateButton");
    
    // Assert data
    tester.assertRenderedPage(AlertsPage.class);
    tester.assertContains("Alerts started Successfully");
  }
  
  /**
   * Tests the links on the Alerts Page class.
   */
  @Test
  public void testLinks() {
  
    // Start up the WicketTester and check that the startpage renders. 
    WicketTester tester = new WicketTester(new DueDatesApplication(config));
    tester.startPage(AlertsPage.class);
    
    assertNotNull("Testing wicket:id Alerts found", tester.getTagByWicketId(ALERTS));
    
    //click and assert links.
    tester.clickLink(ALERTS);
    tester.assertRenderedPage(AlertsPage.class);
    tester.clickLink("Results");
    tester.assertRenderedPage(ResultPage.class);
    tester.clickLink("Logout");
    tester.assertRenderedPage(Index.class);
    
  }
   
}
